package com.example.homework8.adapter;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import androidx.fragment.app.FragmentActivity;

import com.example.homework8.R;
import com.example.homework8.bean.PlanInfo;
import com.example.homework8.bean.RecordInfo;
import com.example.homework8.database.PlanDBHelper;
import com.example.homework8.database.RecordDBHelper;
import com.example.homework8.fragment.PlanEditFragment;
import com.example.homework8.fragment.RecordEditFragment;

import java.util.ArrayList;

public class PlanAdapter extends BaseAdapter {

    private ArrayList<RecordInfo> mData;
    private FragmentActivity mContext;

    public PlanAdapter(ArrayList<RecordInfo> mData, FragmentActivity mContext) {
        this.mData = mData;
        this.mContext = mContext;
    }
    public void setmData(ArrayList<RecordInfo> mData){
        this.mData = mData;
    }
    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = LayoutInflater.from(mContext).inflate(R.layout.plan_item,parent,false);
        TextView item_title = (TextView) convertView.findViewById(R.id.item_title);
        TextView item_cate = (TextView) convertView.findViewById(R.id.item_cate);
        TextView item_createtime = (TextView) convertView.findViewById(R.id.item_createtime);
        TextView item_desc = (TextView) convertView.findViewById(R.id.item_desc);
        item_title.setText(mData.get(position).name);
        item_cate.setText(mData.get(position).cate);
        item_createtime.setText(mData.get(position).createtime);
        item_desc.setText(mData.get(position).info.length() > 10 ? mData.get(position).info.substring(0,10):mData.get(position).info );
        Button button = convertView.findViewById(R.id.delbtn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                PlanDBHelper recordhelper = PlanDBHelper.getInstance(mContext, 1);
                recordhelper.openReadLink();
                recordhelper.delete("_id=" + mData.get((int)position -1).rowid );
                mData.remove(position);
                notifyDataSetChanged();
            }
        });
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RecordEditFragment recordEditFragment = new RecordEditFragment(mData.get((int)position));
                mContext.getSupportFragmentManager()
                        .beginTransaction()
                        .replace(R.id.id_content, recordEditFragment, null)
                        .addToBackStack(null)
                        .commit();
            }
        });

        convertView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                AlertDialog.Builder bb = new AlertDialog.Builder(mContext);
                bb.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        RecordDBHelper recordhelper = RecordDBHelper.getInstance(mContext, 1);
                        recordhelper.openReadLink();
                        recordhelper.delete("_id=" + mData.get((int)position).rowid);
                        mData.remove(position);
                        notifyDataSetChanged();
                    }
                });

                bb.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                });
                bb.setMessage("是否删除记录");
                bb.setTitle("提示");
                bb.show();
                return true;
            }
        });
        return convertView;
    }
}
